<?php
/**
* @desc 商品表单处理
* @author kevin.wang
* @date 2015-01-01 12:00:00
**/
class GoodsForm extends BaseForm
{
	public $goods_name;
	public $goods_category_id;
	public $theme_id;
	public $goods_sub;
	public $price;
	public $sort;
	public $unit;
	public $is_show;
	public $image;
	public $detail_image;
	public $goods_intro;
	public $const_intro;
	public $attentions;
	//表单规则
	public function rules()
	{
		return array(
			array('goods_name, goods_category_id, theme_id, goods_sub, price, sort, unit, image, detail_image', 'required', 'message'=>'必填字段不能为空'),
			array('goods_name', 'unique' , 'on'=>'add', 'className'=>'Goods', 'message'=>'商品名称已存在'),
			array('sort', 'numerical', 'message'=>'排序必须为数字'),
			array('is_show', 'boolean', 'message'=>'是否显示为布尔值'),
			array('goods_intro','default', 'setOnEmpty'=>true, 'value'=>''),
			array('const_intro','default', 'setOnEmpty'=>true, 'value'=>''),
			array('attentions','default', 'setOnEmpty'=>true, 'value'=>''),
		);
	}
	//写入数据
	public function saveData(){
		$model = new Goods;
		$userInfo = json_decode(Yii::app()->session['userinfo'], true);
		$data = $this->attributes;
		$data['goods_intro'] = htmlspecialchars($data['goods_intro']);
		$data['const_intro'] = htmlspecialchars($data['const_intro']);
		$data['attentions'] = htmlspecialchars($data['attentions']);
		$data['author'] = $userInfo['username'];
		$data['goods_sn'] = $this->getGoodsSn();
		return $model->insertGoods($data);
	}
	//修改数据
	public function editData($id){
		$data = $this->attributes;
		$data['goods_intro'] = htmlspecialchars($data['goods_intro']);
		$data['const_intro'] = htmlspecialchars($data['const_intro']);
		$data['attentions'] = htmlspecialchars($data['attentions']);
		$model = new Goods;
		return $model->editGoods($id, $data);
	}
}
